<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>键盘事件</title>
    <style>
        #result{
            width: 400px;
            height: 300px;
            border:solid 1px #aaa;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{

            state = {
                msg: [],
                sendType: 'enter' // enter  c-enter
            }

            change = (type) => {
                return () => {
                    this.setState({
                        sendType: type
                    })
                }
            }

            render(){
                //获取 msg 的值
                let {msg} = this.state;
                return <div>
                        <input type="text" ref={el => this.input = el}  onKeyDown={this.keydown}  /> <button onClick={this.send}>发送</button>
                        <button onClick={this.change('enter')}>enter</button> 
                        <button onClick={this.change('c-enter')}>ctrl + enter 发送</button>
                        <hr />
                        <div id="result" ref={el => this.result = el}>
                            <ul>
                                {
                                    msg.map((item, index) => {
                                        return <li key={index}>{item}</li>
                                    })
                                }
                            </ul>    
                        </div>
                    </div>
            }

            keydown = e => {
                let {msg, sendType} = this.state; 
                //判断
                // if(e.keyCode === 13){  //按下 enter 进行发送
                if(sendType === 'enter'){
                    if(e.keyCode === 13){ //按下 ctrl + enter 进行发送
                        //获取用户输入的值
                        let v = this.input.value
                        //新元素压入
                        msg.push(v);
                        //设置
                        this.setState({
                            msg: msg
                        })
                    }
                }else{
                    if(e.keyCode === 13 && e.ctrlKey){ //按下 ctrl + enter 进行发送
                        //获取用户输入的值
                        let v = this.input.value
                        //新元素压入
                        msg.push(v);
                        //设置
                        this.setState({
                            msg: msg
                        })
                    }
                }
                
            }

            send = () => {
                let {msg} = this.state;
                //获取用户输入的值
                let v = this.input.value
                //新元素压入
                msg.push(v);
                //设置
                this.setState({
                    msg: msg
                })
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>